package com.cis467.mrball;

import java.util.ArrayList;

import com.cis467.mrball.backend.LocalContent;
import com.cis467.mrball.parts.Level;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MrBall extends Activity implements OnTouchListener{

	private static final int MENU_RESUME = 0;

	private static final int MENU_RESTART = 1;
	
	private static final int MENU_CALIBRATE = 2;
	
	private static final int MENU_QUIT = 3;
	    
	/** A handle to the View in which the game is running. */
    private MrBallView mMrBallView;
    
    private ArrayList<Level> game;
    
    private String mode;
    
    /**
     * Invoked during init to give the Activity a chance to set up its Menu.
     * 
     * @param menu the Menu to which entries may be added
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_RESUME, 0, "Resume");
        menu.add(0, MENU_RESTART, 0, "Restart");
        menu.add(0, MENU_CALIBRATE, 0, "Calibrate");
        menu.add(0, MENU_QUIT, 0, "Quit");
        
      

        return true;
    }

    /**
     * Invoked when the user selects an item from the Menu.
     * 
     * @param item the Menu entry which was selected
     * @return true if the Menu item was legit (and we consumed it), false
     *         otherwise
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_RESUME:
            	mMrBallView.getThread().unpause();
                return true;
            case MENU_RESTART:
            	mMrBallView.getThread().restartLevel();
            	mMrBallView.getThread().unpause();
                return true;
            case MENU_CALIBRATE:
            	mMrBallView.getThread().calibrate();
            	mMrBallView.getThread().unpause();
            	return true;
            case MENU_QUIT:
            	finish();
                return true;
        }

        return false;
    }
    
    /**
     * Invoked when the Activity is created.
     * 
     * @param savedInstanceState a Bundle containing state saved from a previous
     *        execution, or null if this is a new execution
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        
        // tell system to use the layout defined in our XML file
        setContentView(R.layout.mrball_layout);
        
        // get handles to view, thread, and sensor
        mMrBallView = (MrBallView) findViewById(R.id.mrball);
        
        // give the view a handle to the saved instance state
        // this may be null, but it will be checked later
        mMrBallView.setSavedInstanceState(savedInstanceState);
        
        mMrBallView.setFocusableInTouchMode(true);
        mMrBallView.setOnTouchListener(this); 
        
        mMrBallView.setActivity(this);
        
        game = new ArrayList<Level>();
        
        Intent intent = getIntent();
        
        mode = intent.getStringExtra("mode");
        
        if(mode.equals("Tutorial")) {
        	game = LocalContent.get_coreLevels();
        	
//        	for(int i = 0; i < game.size()-1; i++)
//        		for(int j = i+1; j < game.size(); j++)
//        			if(game.get(i).getName().compareTo(game.get(j).getName()) > 0) {
//        				Level temp = game.get(i);
//        				game.set(i, game.get(j));
//        				game.set(j, temp);
//        			}
        	
//        	ArrayList<Level> temp = new ArrayList<Level>();
//        	temp.add(game.remove(0));
//        	temp.add(game.remove(0));
//        	game = temp;
        }
        else
        	try {
        		 game.add(LocalContent.convertXML(intent.getStringExtra("levelXML")));
        	}
        	catch(Exception e) {
        		
        	}
    }

    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     * 
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
        mMrBallView.getThread().saveState(outState);
    }
    
    public ArrayList<Level> getGame() {
    	return game;
    }
    
    public String getMode() {
    	return mode;
    }
    
    public boolean onTouch(View v, MotionEvent event) {

		int action = event.getAction();

		if(action == MotionEvent.ACTION_DOWN) {
			
			mMrBallView.getThread().handleTouch((int) event.getX(), (int) event.getY());	
		}
		return true;
	}
}